package com.rmd.ai.grpc.test;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.ByteString;
import com.rmd.ai.grpc.dto.AbilityAsrResultDTO;
import com.rmd.ai.grpc.handler.AbilityGrpcCallback;
import com.rmd.ai.grpc.handler.AbilityGrpcHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class ThreadTask extends Thread{
    private int seq;
    private Long appId = 1L;
    private String appSecret = "";
    private Integer frameRate = 16000;
    private byte[] audioData = null;
    private AbilityGrpcHandler grpcHandler;
    public ThreadTask(AbilityGrpcHandler grpcHandler, int seq, Long appId, String appSecret,Integer frameRate, byte[] audioData){
        this.seq = seq;
        this.grpcHandler =grpcHandler;
        this.appId = appId;
        this.appSecret = appSecret;
        this.frameRate = frameRate;
        this.audioData = audioData;
    }

    @Override
    public void run() {
        int finalSeq = this.seq;
        CountDownLatch latch = new CountDownLatch(1);
        grpcHandler.asrForPool(appId,appSecret,frameRate, ByteString.copyFrom(audioData), new AbilityGrpcCallback() {
            @Override
            public void callback(String result) {
                AbilityAsrResultDTO asr = JSON.parseObject(result,AbilityAsrResultDTO.class);
                log.info("seq:"+finalSeq+" asr:"+asr.getText());

            }

            @Override
            public void completed() {
                latch.countDown();
            }
        });
        try {
            latch.await();
        }catch (Exception e){
            log.error("",e);
        }
    }
}
